﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Common;

public partial class Master_Admin_AddAgent :Admin_BasePage
{
    protected AgentModel agentModel = new AgentModel();
    protected int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (!IsPostBack)
        {
            this.agentModel.LoginPro = 23;
            this.agentModel.LoginCity =195;
            if (string.IsNullOrEmpty(Request["id"]))
            {
                id = 0;
            }
            else
            {
                id = StringHelper.isNumber_int(Request["id"]);
            }
            if(id!=0)
            {
                this.agentModel=Agen_BLL.CreateNew.GetModel(this.id);
                ViewState["agentModel"] = agentModel;
                displayAgentInfo(this.agentModel);
            }
        }
        else
        {
            if (ViewState["agentModel"]!=null)
            {
                this.agentModel = ViewState["agentModel"] as AgentModel;
            }
        }
        if (id != 0)
        {
            btnSubmit.Visible = false;
            Button1.Visible = true;
            lblTip.Visible = true;
            lblTip1.Visible = true;
            lblTitle.Text = "编辑代理商信息";
            lblTitle1.Text = "编辑代理商信息";
        }
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ExistAgentUserName(txtAgentName.Text))
        {
            Page.RegisterStartupScript("ExistAgentUserName", "<script>alert('数据库中已经存在该代理名，请输入其它的代理名!');</script>");
            return;
        }
        if (ExistAgent(StringHelper.isNumber_int(Request["selProvice"]), StringHelper.isNumber_int(Request["selCity"])))
        {
            Page.RegisterStartupScript("ExistAgent", "<script>alert('该城市已经有代理商，请选择其它城市!');</script>");
            return;
        }
        if (AddAgent())
        {
            ResultStr("Admin_ManageAgent.aspx", "添加成功!");
        }
        else
        {
            ResultStr("Admin_ManageAgent.aspx", "添加失败!");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtAgentName.Text.Trim()!=this.agentModel.AgentUserName)
        {
            if (ExistAgentUserName(txtAgentName.Text))
            {
                Page.RegisterStartupScript("ExistAgentUserName", "<script>alert('数据库中已经存在该代理名，请输入其它的代理名!');</script>");
                return;
            }
        }
        if (StringHelper.isNumber_int(Request["selProvice"]) != this.agentModel.LoginPro || StringHelper.isNumber_int(Request["selCity"]) != this.agentModel.LoginCity)
        {
            if (ExistAgent(StringHelper.isNumber_int(Request["selProvice"]), StringHelper.isNumber_int(Request["selCity"])))
            {
                Page.RegisterStartupScript("ExistAgent", "<script>alert('该城市已经有代理商，请选择其它城市!');</script>");
                return;
            }
        }
        
        if (UpdateAgent())
        {
            ResultStr("Admin_ManageAgent.aspx", "保存成功!");
        }
        else
        {
            ResultStr("Admin_ManageAgent.aspx", "保存失败!");
        }
    }

    public void displayAgentInfo(AgentModel agentModel)
    {
        txtAddress.Text = agentModel.Address;
        txtAgentName.Text = agentModel.AgentUserName;
        txtCompany.Text = agentModel.Company;
        txtEmail.Text = agentModel.Email;
        txtPwd.Text = agentModel.AgentPwd;
        txtPwdAgain.Text = agentModel.AgentPwd;
        txtTel.Text = agentModel.Tel;
        txtTrueName.Text = agentModel.TrueName;
    }

    public bool ExistAgent(int province,int city)
    {
        string strWhere = " LoginPro={0} and LoginCity={1}";
        strWhere = string.Format(strWhere,province,city);
        DataTable dataTable = new DataTable();
        dataTable = Agen_BLL.CreateNew.GetDataTable(strWhere);
        return dataTable.Rows.Count == 0 ? false : true;
    }

    public bool ExistAgentUserName(string AgentUserName)
    {
        string strWhere = " AgentUserName='{0}'";
        strWhere = string.Format(strWhere, AgentUserName);
        DataTable dataTable = new DataTable();
        dataTable = Agen_BLL.CreateNew.GetDataTable(strWhere);
        return dataTable.Rows.Count == 0 ? false : true;
    }

    public bool UpdateAgent()
    {

        agentModel.Address = txtAddress.Text;
        agentModel.AgentPwd =(txtPwd.Text==""?this.agentModel.AgentPwd:EncryptString.md5(txtPwd.Text));
        agentModel.AgentUserName = txtAgentName.Text;
        agentModel.Company = txtCompany.Text;
        agentModel.Disu = this.agentModel.Disu;
        agentModel.Email = txtEmail.Text;
        agentModel.Fax = this.agentModel.Fax;
        agentModel.Forms = this.agentModel.Forms;
        agentModel.IsCheck = this.agentModel.IsCheck;
        agentModel.IsLock = this.agentModel.IsLock;
        agentModel.LoginCity = StringHelper.isNumber_int(Request["selCity"]);
        agentModel.LoginCount = this.agentModel.LoginCount;
        agentModel.LoginIP = "";
        agentModel.LoginPro = StringHelper.isNumber_int(Request["selProvice"]);
        agentModel.LoginTime = DateTime.Now;
        agentModel.Purview = this.agentModel.Purview;
        agentModel.RegTime = this.agentModel.RegTime;
        agentModel.Tel = txtTel.Text;
        agentModel.TrueName = txtTrueName.Text;
        agentModel.Zip = this.agentModel.Zip;


        return Agen_BLL.CreateNew.UpdateAgent(agentModel)>0?true:false;
        
    }

    public bool AddAgent()
    {
        AgentModel agentModel = new AgentModel();
        agentModel.Address = txtAddress.Text;
        agentModel.AgentPwd = EncryptString.md5(txtPwd.Text);
        agentModel.AgentUserName = txtAgentName.Text;
        agentModel.Company = txtCompany.Text;
        agentModel.Disu = 0;
        agentModel.Email = txtEmail.Text;
        agentModel.Fax = "";
        agentModel.Forms = 0;
        agentModel.IsCheck = 0;
        agentModel.IsLock = 0;
        agentModel.LoginCity = StringHelper.isNumber_int(Request["selCity"]);
        agentModel.LoginCount = 0;
        agentModel.LoginIP = "";
        agentModel.LoginPro = StringHelper.isNumber_int(Request["selProvice"]);
        agentModel.LoginTime = DateTime.Now;
        agentModel.Purview = "";
        agentModel.RegTime = DateTime.Now;
        agentModel.Tel = txtTel.Text;
        agentModel.TrueName = txtTrueName.Text;
        agentModel.Zip = "";
        return Agen_BLL.CreateNew.AddAgent(agentModel) > 0 ? true : false;
    }
}
